package com.jspgou.cms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ShopTuijanproductDao;
import com.jspgou.cms.dao.ShopTuijianDao;
import com.jspgou.cms.entity.ShopTuijanproduct;
import com.jspgou.cms.entity.ShopTuijian;
import com.jspgou.cms.entity.Product;

@Repository
public class ShopTuijanproductDaoImpl extends HibernateBaseDao<ShopTuijanproduct, Long> implements ShopTuijanproductDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}

	public ShopTuijanproduct findById(Long id) {
		ShopTuijanproduct entity = get(id);
		return entity;
	}

	public ShopTuijanproduct save(ShopTuijanproduct bean) {
		getSession().save(bean);
	/*	getSession().evict(bean.getTuijianid());*/
		return bean;
	}

	public ShopTuijanproduct deleteById(Long id) {
		ShopTuijanproduct entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<ShopTuijanproduct> getEntityClass() {
		return ShopTuijanproduct.class;
	}

	public List<ShopTuijanproduct> getShopTuijanproducts(Product product,
			ShopTuijian tuijian) {
		Finder f=Finder.create("from ShopTuijanproduct bean where bean.productid=:productid  and bean.tuijianid=:tuijianid");
		f.setParam("productid", product);
		f.setParam("tuijianid", tuijian);
		return find(f);
	}

	public Pagination getBytuijian(Long id,int pageNo, int pageSize) {
	
		
		Finder f=Finder.create("from ShopTuijanproduct bean where 1=1 ");
		if(id!=null){
			ShopTuijian tuijian=tuijianDao.findById(id);
			f.append(" and bean.tuijianid=:id");
			f.setParam("id", tuijian);
			
		}
		f.append(" order by tjorder asc");
		return find(f, pageNo, pageSize);
	}
	
	public List<ShopTuijanproduct> getList(Long id, int count) {

		Finder f=Finder.create("from ShopTuijanproduct bean where 1=1 ");
		if(id!=null){
			ShopTuijian tuijian=tuijianDao.findById(id);
			f.append(" and bean.tuijianid=:id");
			f.setParam("id", tuijian);
			
		}
		f.append(" order by tjorder asc");
		f.setMaxResults(count);
		return find(f);
	}
	@Autowired 
	private ShopTuijianDao tuijianDao;
	
}